package com.framework.modules.job.task.yhbtask;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.entity.ter.TerTerminalInfo;
import com.framework.service.yhbservice.YhbCurrentCountService;
import com.framework.service.yhbservice.YhbRecommendMerchantService;
import com.framework.service.yhbservice.YhbTerminalService;
import com.framework.service.yhbservice.YhbUserService;
import com.framework.utils.DateUtils;
import com.framework.utils.string.StringUtil;
import com.framework.yhbEntity.currentcount.YhbCurrentCount;
import com.framework.yhbEntity.user.YhbRecommendMerchant;
import com.framework.yhbEntity.user.YhbUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 激活商户
 * @author wq
 */
@Component("activRecommend")
public class ActivRecommend {


    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private YhbRecommendMerchantService yhbRecommendMerchantService;

    @Autowired
    private YhbTerminalService yhbTerminalService;

    @Autowired
    private YhbUserService yhbUserService;


    @Autowired

    private YhbCurrentCountService yhbCurrentCountService;

    public void activRecomment() {

        logger.debug("-------清算激活商户奖励开始----------");
        List<YhbRecommendMerchant> list = yhbRecommendMerchantService.selectList(
                new EntityWrapper<YhbRecommendMerchant>()
                        .eq("state", 1));
        //当天
        Date today = DateUtils.addDateDays(new Date(), 0);
        for (YhbRecommendMerchant recommendMerchant :list) {

            TerTerminalInfo t = yhbTerminalService.queryTerminalInfoBySn(recommendMerchant.getSn());
            if (t == null) {
                logger.debug(recommendMerchant.getSn() + "未查到机具");
                continue;
            }
            //核算已激活机器
            if (t.getIsActivation().intValue() == 2) {
                //当天激活，当天返现
                if (t.getActivationDate().compareTo(today)==0) {

                    //已返现
                    YhbCurrentCount yhbCurrentCount = yhbCurrentCountService.selectOne(new EntityWrapper<YhbCurrentCount>().eq("relation_id", recommendMerchant.getId())
                            .eq("income", 1)
                            .eq("income_type", 5)
                    );

                    if(yhbCurrentCount==null){
                        YhbUser user = yhbUserService.queryUserByUserId(recommendMerchant.getUserId());
                        //添加余额
                        yhbUserService.addAmount(user,new BigDecimal("50"));

                        //记录
                        YhbCurrentCount currentCount=new YhbCurrentCount();
                        //导入流水
                        String accountId = StringUtil.getDateString(6);
                        accountId=verifyAccountId(accountId);
                        currentCount.setMoney(new BigDecimal("0.00"));
                        currentCount.setIsActivity(0);
                        currentCount.setUserId(user.getUserId());
                        currentCount.setRelationId(String.valueOf(recommendMerchant.getId()));
                        currentCount.setIncome(1);
                        currentCount.setIncomeType(5);
                        currentCount.setCreateDate(new Date());
                        currentCount.setAccountId(accountId);

                        yhbCurrentCountService.insert(currentCount);
                    }

                }
            }


        }
        logger.debug("-------清算激活商户奖励结束----------");
    }
   private String  verifyAccountId(String accountId){
       YhbCurrentCount t = yhbCurrentCountService.selectById(accountId);
       if(t!=null){
           accountId=StringUtil.getDateString(6);
           verifyAccountId(accountId);
       }
       return accountId;
    }


}
